package com.ruoyi.car.controller.admin.carPaymentMethod;

import com.github.pagehelper.PageInfo;
import com.ruoyi.car.controller.admin.carPaymentMethod.vo.CarPaymentMethodAddReq;
import com.ruoyi.car.controller.admin.carPaymentMethod.vo.CarPaymentMethodEditReq;
import com.ruoyi.car.controller.admin.carPaymentMethod.vo.CarPaymentMethodResp;
import com.ruoyi.car.controller.admin.model.vo.CarModelAddReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelEditReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelResp;
import com.ruoyi.car.entity.CarModel;
import com.ruoyi.car.entity.CarPaymentMethod;
import com.ruoyi.car.mapstruct.CarModelConvert;
import com.ruoyi.car.mapstruct.CarPaymentMethodTo;
import com.ruoyi.car.service.CarModelService;
import com.ruoyi.car.service.CarPaymentMethodService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageReq;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author：流冬
 * @Package：com.ruoyi.car.controller.admin.carPaymentMethod
 * @Project：RuoYi-Vue
 * @name：CarPaymentMethodController
 * @Date：2023/6/5 21:37
 * @Filename：CarPaymentMethodController
 * @Description
 */
@Api(tags = "管理后台-收费")
// springmvc的 @Controller+@ResponseBody
@RestController
// springmvc注解
@RequestMapping(value = "/car/carPaymentMethod")
public class CarPaymentMethodController {

    @Autowired
    private CarPaymentMethodService carPaymentMethodService;


    // spring security权限
    @PreAuthorize(value = "@ss.hasPermi('car:carPaymentMethod:page')")
    // springmvc  get请求注解
    @GetMapping(value = "/page")
    // swagger3 的注解
    // @Operation(summary = "获取分页列表")
    @ApiOperation(value = "获取分页列表")
    public TableDataInfo list(PageReq req){
        PageInfo<CarPaymentMethod> pageInfo = this.carPaymentMethodService.search(req);

        //总记录数
        long count= pageInfo.getTotal();
        //转换完成以后的结果
        List<CarPaymentMethodResp> list= CarPaymentMethodTo.INSTANCE.convert(pageInfo.getList());

        TableDataInfo tableDataInfo=new TableDataInfo(list,count);
        return tableDataInfo;

    }

    @Log(title = "收款方式",businessType = BusinessType.INSERT)
    @PreAuthorize(value = "@ss.hasPermi('car:carPaymentMethod:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid CarPaymentMethodAddReq req){
        CarPaymentMethod convert = CarPaymentMethodTo.INSTANCE.convert(req);
        boolean save = this.carPaymentMethodService.save(convert);
        return save? R.ok(): R.fail();
    }

    @PreAuthorize(value = "@ss.hasPermi('car:carPaymentMethod:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/{id}")
    public R<CarPaymentMethodResp> findById(@PathVariable(value = "id") Long id){
        CarPaymentMethod db = this.carPaymentMethodService.getById(id);
        if(db==null){
            //国际化 i18n 中查询
            String msg= MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarPaymentMethodResp convert = CarPaymentMethodTo.INSTANCE.convert(db);
        return R.ok(convert);
    }

    @Log(title = "收款方式",businessType = BusinessType.UPDATE)
    @PreAuthorize(value = "@ss.hasPermi('car:carPaymentMethod:edit')")
    @ApiOperation(value = "修改保存")
    @PutMapping
    public R  update(@RequestBody @Valid CarPaymentMethodEditReq req){
        CarPaymentMethod db = this.carPaymentMethodService.getById(req.getId());
        if(db==null){
            //国际化 i18n 中查询
            String msg=MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarPaymentMethod convert = CarPaymentMethodTo.INSTANCE.convert(req);

        boolean save = this.carPaymentMethodService.updateById(convert);
        return save? R.ok(): R.fail();
    }
}
